﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util; // Logging

namespace Moogle
{
    /// <summary>
    /// Program class. The entry point for the application. It target consumer is the operating system.  
    /// </summary>
    [Activity(Label = "moogle", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Holo.Light.Dialog", NoHistory = true)]
    [IntentFilter(new[] { Intent.ActionMain })]
    public class Program : ListActivity
    {
        #region Local Memory
        private const int __C_ACTIVITY_KNOWLEDGE=0;
        private const int __C_ACTIVITY_FILL=1;
        private const int __C_ACTIVITY_REMIND=2;
        private const int __C_ACTIVITY_SCAN=3;
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string __C_TAG = "Program";
        #endregion
        #region OnCreate Control Function
        /// <summary>
        /// Class (or Activity) entry point.
        /// </summary>
        /// <param name="bundle">Android.OS.Bundle object instance that can be used to pass data to activity at startup.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Log.Info(__C_TAG, "OnCreate started.");

            try
            {
                String[] lArrMainMenu = Resources.GetStringArray(Resource.Array.program_main_array);
                this.ListAdapter = new ArrayAdapter<String>(this, Resource.Layout.list_item_textview, lArrMainMenu);
            }  // end try
            catch (SystemException err)
            {
                Log.Error(__C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(__C_TAG, "OnCreate ended.");
            } // end finally

        }
        #endregion
        #region Event Handlers
        /// <summary>
        /// Control function used to respond to (or handle) the List Click event. 
        /// </summary>
        /// <param name="l">Android ListView Widget object instance</param>
        /// <param name="v">Android View object instance</param>
        /// <param name="position">integer value defining the position (entry number) of the List item that was clicked</param>
        /// <param name="id">long value defining the id of the entry that was clicked</param>
        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            base.OnListItemClick(l, v, position, id);

            Log.Info(__C_TAG, "Program.OnListItemClick started.");

            try
            {
                switch (position)
                {
                    case __C_ACTIVITY_KNOWLEDGE:
                        var lIntentLearn = new Intent(this, typeof(ActivityLearn));
                        StartActivity(lIntentLearn);
                        break;
                    case __C_ACTIVITY_FILL:
                        var lIntentFill = new Intent(this, typeof(ActivityFill));
                        StartActivity(lIntentFill);
                        break;
                    case __C_ACTIVITY_REMIND:
                        var lIntentRemind = new Intent(this, typeof(ActivityRemind));
                        StartActivity(lIntentRemind);
                        break;
                    case __C_ACTIVITY_SCAN:
                        Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 
                        intent.SetPackage( "com.google.zxing.client.android" );

                        intent.PutExtra("SCAN_MODE", "QR_CODE_MODE"); 
                        StartActivityForResult(intent, 0); 
                        break;
                } // end switch
            } // end try
            catch (SystemException err)
            {
                Log.Error(__C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(__C_TAG, "Program.OnListItemClick ended.");
            } // end finally
        }
        
        /// <summary>
        /// base class override used to respond to the data returned when
        /// the user intiates the scan import process.
        /// </summary>
        /// <param name="requestCode">integer value storing the request code.</param>
        /// <param name="resultCode">Android.App.Result instance indicating whether or not the scan was successful.</param>
        /// <param name="data">Android.Content.Intent instance containing the data returned by the scan process</param>
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            Log.Info(__C_TAG, "OnActivityResult started.");

            try
            {
                if (requestCode == 0)
                {
                    if (resultCode == Result.Ok)
                    {
                        String contents = data.GetStringExtra("SCAN_RESULT");
                        String format = data.GetStringExtra("SCAN_RESULT_FORMAT");
                        Toast.MakeText(this, "Downloading Update ..." + contents, ToastLength.Long).Show();
                    } // end if
                } // end if 

            }  // end try
            catch (SystemException err)
            {
                Log.Error(__C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(__C_TAG, "OnActivityResult ended.");
            } // end finally

        }

     
        #endregion

        


    }
}

